package com.beetobe.callib.event;

import java.io.IOException;

import com.beetobe.callib.CalendarLibHelper;
import com.beetobe.callib.test.EventListActivity;




import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.util.Log;

public class AsyncDeleteEvent extends AsyncTask<Void, Void, Void> {

	private final EventListActivity EventListActivity;
	private final ProgressDialog dialog;
	private final String calendarId;
	private final int eventIndex;
	private final String eventId;
	private com.google.api.services.calendar.Calendar client;

	public AsyncDeleteEvent(EventListActivity EventListActivity, String calendarId, int eventIndex) {
		this.EventListActivity = EventListActivity;
		this.calendarId = calendarId;
		this.eventIndex = eventIndex;
		client = CalendarLibHelper.client;
		eventId = EventListActivity.eventList.get(eventIndex).id;
		dialog = new ProgressDialog(EventListActivity);
	}

	@Override
	protected void onPreExecute() {
		dialog.setMessage("Deleting event...");
		dialog.show();
	}

	@Override
	protected Void doInBackground(Void... arg0) {
		try {
			client.events().delete(calendarId, eventId).execute();
			EventListActivity.eventList.remove(eventIndex);
		} catch (IOException e) {
			EventListActivity.handleGoogleException(e);
		} finally {
			EventListActivity.onRequestCompleted();
		}
		return null;
	}

	@Override
	protected void onPostExecute(Void result) {
		dialog.dismiss();
		EventListActivity.refresh();
	}
}